package com.jxc.jxc01.yk.dao;


import com.jxc.jxc01.yk.entity.OutWarehouse;
import com.jxc.jxc01.yk.entity.PageHelper;
import com.jxc.jxc01.yk.util.JDBCConn;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OutWarehouseDaoImpl implements IOutWarehouseDao {

    /**
     * 增加出库清单
     */
    public boolean addOutWarehouse(List<OutWarehouse> outWarehouses) {
        ResultSet rs = null;
        try {

            //3.插入
            String sql = "insert into outwarehouse(outwarehouse_num,warehouse_num,out_commodity_name,specification," +
                    "out_warehouse_count,outbound_unit_price,totalprice," +
                    "warehouse_person,whether_outbound,create_time,outbound_time) values (?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt = JDBCConn.conn(sql);
            int rows = 0;
            for (OutWarehouse outWarehouse : outWarehouses) {
                pstmt.setInt(1, outWarehouse.getOutwarehouse_num());
                pstmt.setString(2, outWarehouse.getWarehouse_num());
                pstmt.setString(3,outWarehouse.getOut_commodity_name());
                pstmt.setString(4, outWarehouse.getSpecification());
                pstmt.setInt(5, outWarehouse.getOutwarehouse_count());
                pstmt.setDouble(6, outWarehouse.getOutbound_unit_price());
                pstmt.setDouble(7, outWarehouse.getTotalprice());
                pstmt.setString(8, outWarehouse.getWarehouse_person());
                pstmt.setString(9, outWarehouse.getWhether_outbound());
                pstmt.setString(10, outWarehouse.getCreate_time());
                pstmt.setString(11, outWarehouse.getOutbound_time());
                rows = pstmt.executeUpdate();
                if (rows == 0) {
                    //添加失败
                    return false;
                }
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    /**查询出库清单*/
    public List<OutWarehouse> findOutWarehouseList(PageHelper<OutWarehouse> pageHelper) {
        //将查询到的销售清单信息存储集合中
        List<OutWarehouse> outWarehouses = new ArrayList<>();
        ResultSet rs = null;
        try {
            //3.查询销售表
            //分页查询
            String sql = "select count(*) total from outwarehouse";
            PreparedStatement pstmt = JDBCConn.conn(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                int total = rs.getInt("total");
                pageHelper.setTotal(total);
            }
            //总页数
            pageHelper.setPageCount(pageHelper.getTotal()%pageHelper.getPageSize()==0?
                    pageHelper.getTotal()/pageHelper.getPageSize():pageHelper.getTotal()/pageHelper.getPageSize()+1);
            //3.查询表
            sql = "select * from outwarehouse limit ?,?";
             pstmt = JDBCConn.conn(sql);
            pstmt.setInt(1, (pageHelper.getPageNum()-1)*pageHelper.getPageSize());
            pstmt.setInt(2, pageHelper.getPageSize());
            rs = pstmt.executeQuery();
            while (rs.next()) {
                OutWarehouse outWarehouse = new OutWarehouse();
                outWarehouse.setId(rs.getInt(1));
                outWarehouse.setOutwarehouse_num(rs.getInt(2));
                outWarehouse.setWarehouse_num(rs.getString(3));
                outWarehouse.setOut_commodity_name(rs.getString(4));
                outWarehouse.setSpecification(rs.getString(5));
                outWarehouse.setOutwarehouse_count(rs.getInt(6));
                outWarehouse.setOutbound_unit_price(rs.getDouble(7));
                outWarehouse.setTotalprice(rs.getDouble(8));
                outWarehouse.setWarehouse_person(rs.getString(9));
                outWarehouse.setWhether_outbound(rs.getString(10));
                outWarehouse.setCreate_time(rs.getString(11));
                outWarehouse.setOutbound_time(rs.getString(12));
                outWarehouses.add(outWarehouse);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return outWarehouses;
    }

    /**根据出库单号查找某一条记录*/
    public List<OutWarehouse> findOutWarehouseNumber(int Outwarehouse_num) {
        //将查询到的销售清单信息存储集合中
        List<OutWarehouse> outWarehouses = new ArrayList<>();
        ResultSet rs = null;
        try {
            String sql = "select * from outwarehouse where outwarehouse_num=?";
            PreparedStatement pstmt = JDBCConn.conn(sql);
            pstmt.setInt(1, Outwarehouse_num);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                OutWarehouse outWarehouse = new OutWarehouse();
                outWarehouse.setId(rs.getInt(1));
                outWarehouse.setOutwarehouse_num(rs.getInt(2));
                outWarehouse.setWarehouse_num(rs.getString(3));
                outWarehouse.setOut_commodity_name(rs.getString(4));
                outWarehouse.setSpecification(rs.getString(5));
                outWarehouse.setOutwarehouse_count(rs.getInt(6));
                outWarehouse.setOutbound_unit_price(rs.getDouble(7));
                outWarehouse.setTotalprice(rs.getDouble(8));
                outWarehouse.setWarehouse_person(rs.getString(9));
                outWarehouse.setWhether_outbound(rs.getString(10));
                outWarehouse.setCreate_time(rs.getString(11));
                outWarehouse.setOutbound_time(rs.getString(12));
                outWarehouses.add(outWarehouse);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return outWarehouses;
    }

    /**删除出库清单*/
    public boolean deleteOutWarehouse(int id) {
        ResultSet rs = null;
        try {
            //3.查询销售表
            String sql = "delete from outwarehouse where id=?";
            PreparedStatement pstmt = JDBCConn.conn(sql);
            pstmt.setInt(1, id);
            int rows = pstmt.executeUpdate();
            if (rows == 0) {
                //删除失败
                return false;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    /**修改出库清单*/
    public boolean updateOutWarehouse(OutWarehouse outWarehouse) {
        ResultSet rs = null;
        try {
            //3.修改出库表
            String sql = "update outwarehouse set warehouse_num=?,out_commodity_name=?,specification=?," +
                    "out_warehouse_count=?,outbound_unit_price=?,totalprice=?,warehouse_person=?," +
                    "whether_outbound=?,outbound_time=? where id=?";
            PreparedStatement pstmt = JDBCConn.conn(sql);
            int rows = 0;
            pstmt.setString(1, outWarehouse.getWarehouse_num());
            pstmt.setString(2,outWarehouse.getOut_commodity_name());
            pstmt.setString(3, outWarehouse.getSpecification());
            pstmt.setInt(4, outWarehouse.getOutwarehouse_count());
            pstmt.setDouble(5, outWarehouse.getOutbound_unit_price());
            pstmt.setDouble(6, outWarehouse.getTotalprice());
            pstmt.setString(7, outWarehouse.getWarehouse_person());
            pstmt.setString(8, outWarehouse.getWhether_outbound());
            pstmt.setString(9, outWarehouse.getOutbound_time());
            pstmt.setInt(10, outWarehouse.getId());
                rows = pstmt.executeUpdate();
                if (rows == 0) {
                    //修改失败
                    return false;
                }
        }  catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return true;
    }
}
